package cn.yy.cloudpay.cardread;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;

import org.apache.log4j.Logger;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.math.BigDecimal;
import java.util.HashMap;

import cn.yy.cloudpay.R;
import cn.yy.cloudpay.activity.BaseActivity;
import cn.yy.cloudpay.application.CloudPayApplication;
import cn.yy.cloudpay.common.iface.ResultVo;
import cn.yy.cloudpay.util.Constant;
import cn.yy.cloudpay.view.ToastUtil;

public class GetAmountActivity extends BaseActivity {

    private BigDecimal price;
    private Context mContext;
    private String pan;
    public CloudPayApplication mApplication;
    public Logger log= Logger.getLogger(this.getClass());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_amount);
        mContext = this;
        mApplication = (CloudPayApplication) getApplication();
        Log.d("GetAmountActivity", "进入 GetAmountActivity 。。。");
        pan = getIntent().getStringExtra("pan");
        String cardbin = pan.substring(0,6);
        getPrice("",cardbin);
    }

    private void consume_getAmount() {
        log.info("discountAmount: " + price);
        Intent intent = new Intent();
        try {
            intent.putExtra("amount", price.longValue());//金额统一以分为单位,long类型
        } catch (Exception e) {
            e.printStackTrace();
        }
        setResult(RESULT_OK, intent);
        finish();
    }

    public void getPrice(String auth_code,String payway) {
        try {
            HashMap<String, Object> params = new HashMap<String, Object>();
            params.put("device_info", CloudPayApplication.deviceSN);
            params.put("auth_code", auth_code);
            params.put("pay_way",payway);

            RequestParams params2;
            if(mApplication.getServiceAddress().indexOf("9999")!=-1){
                params2 = httParamsTest(Constant.FEXI_PRICE, params);
            }else{
                params2 = httParams(Constant.FEXI_PRICE, params);
            }
            x.http().post(params2, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String arg0) {
                    ResultVo rv = JSON.parseObject(arg0, ResultVo.class);
                    if ("0".equals(rv.getReturnCode())) {
                        com.alibaba.fastjson.JSONObject params = JSON.parseObject(rv.getReturnParams().toString());
                        if(params.get("pay_amount")==null){
                            ToastUtil.showToast(mContext, "价格获取失败", ToastUtil.FAIL);
                            return;
                        }
                        String amount = params.get("pay_amount").toString();
                        price = new BigDecimal(amount).multiply(new BigDecimal(100));
                        consume_getAmount();
                    }else{
                        ToastUtil.showToast(mContext, rv.getReturnMsg(), ToastUtil.FAIL);
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    log.error("请求失败", ex);
                }

                @Override
                public void onFinished() {
                }

                @Override
                public void onCancelled(CancelledException arg0) {
                }
            });
        }catch (Exception ex){
            log.error("请求异常",ex);
        }
    }

    public RequestParams httParams(String serviceId,HashMap<String, Object> params){
        String paramStr = JSON.toJSONString(params);
        String rs = "{\"serviceId\":\""+serviceId+"\",\"params\":"
                + paramStr + "}";
        RequestParams params2 = new RequestParams(Constant.HTTP_URL);
        params2.setBodyContent(rs);
        return params2;
    }

    public RequestParams httParamsTest(String serviceId,HashMap<String, Object> params){
        String paramStr = JSON.toJSONString(params);
        String rs = "{\"serviceId\":\""+serviceId+"\",\"params\":"
                + paramStr + "}";
        RequestParams params2 = new RequestParams(Constant.HTTP_URL_TEST);
        params2.setBodyContent(rs);
        return params2;
    }
}
